(function (Cofe, Spine, $) {
    var Models = Cofe.Models;
    var File = Models.File;
    var Folder = Models.Folder;

    var EntryList = Spine.Model.setup("EntryList",  //Models name
    ["status", "entries", "path"]);  //Models attributes.

    EntryList.extend(Spine.Model.Local);

    EntryList.include({
        loadEntries: function (entryData) {
            var folders = new Array();
            var files = new Array();
            var folderIdx = 0;
            var fileIdx = 0;

            for (var i = 0, il = entryData.length; i < il; i++) {
                var currentEntry = entryData[i];
                var newEntry;
                if (currentEntry.basetype == "Folder") {
                    newEntry = Folder.init(currentEntry);
                    folders[folderIdx++] = newEntry;
                }
                else {
                    newEntry = File.init(currentEntry);
                    files[fileIdx++] = newEntry;
                }
            }

            this.folders = folders;
            this.files = files;
        },

        load: function (data) {
            this.status = data.status;
            this.entries = data.entries;
            this.loadEntries(data.entries);
        },
        init: function (data) {
            this.path = this.id = data.path;
            this.status = data.status;
            this.entries = data.entries;
            if (data.entries)
                this.loadEntries(data.entries);
            //            console.log("EntryList created: Path=" + data.path);
        }

    });

    EntryList.include({
        url: "",
        fetch: function () {
            var url = "/lookup/" + this.path + "&rel=list&json=true"
            var jqxhr = $.get(url,
                this.proxy(function (data) {
                    this.load(data); //load data.
                    this.save(); //save current entrylist to local storage.
                    EntryList.trigger("refresh", this);
                }))
            //                .success(function () { alert("second success"); })
                .error(function (xhr, s, e) { EntryList.trigger(xhr, s, e); })
            //                .complete(function () { alert("complete"); });
        }
    });

    EntryList.extend({
        lookupOrCreate: function (path, refresh, ajaxErr) {
            var entryList = Cofe.Models.EntryList.init({ path: path });
            EntryList.attachEvents(entryList, refresh, ajaxErr);

            if (entryList.status == undefined && refresh != undefined && ajaxErr != undefined) {
                entryList.fetch();
            }

            return entryList;
        },
        unattachEvents: function (prevEntryList, refresh, ajaxErr) {
            if (prevEntryList != undefined && prevEntryList.unbind != undefined) {
                if (refresh != undefined)
                    prevEntryList.unbind("refresh", refresh);
                if (ajaxError != undefined)
                    prevEntryList.unbind("ajaxError", ajaxErr);
            }
        },
        attachEvents: function (entryList, refresh, ajaxErr) {
            if (refresh != undefined)
                entryList.bind("refresh", refresh);
            if (ajaxErr != undefined)
                entryList.bind("ajaxError", ajaxErr);
        }


    });


    if (typeof exports !== "undefined") {
        Models.EntryList = exports;
    } else {
        Models.EntryList = EntryList;
    }

})(Cofe, Spine, Spine.$);